
public class ZigZag {
	public String convert(String s, int nRows) {
        nR = nRows;
        String [] strArray = new String [nRows];
        
        for(int i =0  ; i < nRows ; i ++)
        {
            strArray[i] = "";
        }
        
        for(int i = 0 ; i < s.length() ; i ++)
        {
            int t = next();
            strArray[t] = strArray[t]+s.charAt(i);
        }
        String result = "";
        for(int i = 0 ; i < nRows ; i ++)
        {
            result = result + strArray[i];
        }
        return result;
    }
    
    int nR = 0;
    int current = 1;
    int direction = 0; // 0: decrease, 1: increase
    
    public int next()
    {
        if(nR == 1) return 0;
        if(direction == 0)
        {
            if(current == 1)
            {
                current = current - 1;
                direction = 1;
            }
            else
            {
                current = current - 1;
            }
        }
        else
        {
            if(current >= nR - 2)
            {
                current = current + 1;
                direction = 0;
            }
            else
            {
                current = current + 1;
            }
        }
        return current;
    }
}
